package com.yylending.las.repay.repaydetail;

import android.os.Parcel;
import android.os.Parcelable;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.YJKD;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ying_he on 2017/6/9.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class RepayDetailListModel extends Model {
	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJKD.getInstance().getPreferenceManager().getLoginToken();
		return service.getRepayList(token, (RequestParams) params);
	}

	@Override
	public void onNext(Object entity) {
		RepayListResult loanListResult = cast(entity);
		EventAgent.post(loanListResult);
	}

	public interface Service {
		@POST("server/repay/planList")
		Observable<RepayListResult> getRepayList(@Header("token") String token, @Body RequestParams requestParams);
	}

	static class RequestParams {
		String loanNo = "";
		String type = "";
	}

	static class RepayListResult extends BaseResultEntity<Repay> {

	}

	static class Repay implements Parcelable {
		String type = "";
		String repayStatus = "";
		List<RepayDetail> detail = new ArrayList<>();


		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(type);
			dest.writeString(repayStatus);
			dest.writeList(detail);
		}

		private Repay(Parcel in) {
			type = in.readString();
			repayStatus = in.readString();
			detail = in.readArrayList(RepayDetail.class.getClassLoader());
		}

		public static final Parcelable.Creator<Repay> CREATOR =
				new Parcelable.Creator<Repay>() {
					public Repay createFromParcel(Parcel in) {
						return new Repay(in);
					}

					public Repay[] newArray(int size) {
						return new Repay[size];
					}
				};

		static class RepayDetail implements Parcelable {
			String loanDate = "";
			String loanAmount = "";
			String noPayTerm = "";
			String loanStatus = "";
			String loanNo = "";
			String term = "";
			String amount = "";
			String repayDate = "";
			String termStatus = "";

			public static final Parcelable.Creator<RepayDetail> CREATOR =
					new Parcelable.Creator<RepayDetail>() {
						public RepayDetail createFromParcel(Parcel in) {
							return new RepayDetail(in);
						}

						public RepayDetail[] newArray(int size) {
							return new RepayDetail[size];
						}
					};

			@Override
			public int describeContents() {
				return 0;
			}

			@Override
			public void writeToParcel(Parcel dest, int flags) {
				dest.writeString(loanDate);
				dest.writeString(loanAmount);
				dest.writeString(noPayTerm);
				dest.writeString(loanStatus);
				dest.writeString(loanNo);
				dest.writeString(term);
				dest.writeString(amount);
				dest.writeString(repayDate);
				dest.writeString(termStatus);
			}

			public RepayDetail(Parcel in) {

				loanDate = in.readString();
				loanAmount = in.readString();
				noPayTerm = in.readString();
				loanStatus = in.readString();
				loanNo = in.readString();
				term = in.readString();
				amount = in.readString();
				repayDate = in.readString();
				termStatus = in.readString();
			}
		}
	}
}
